<?php defined('BASEPATH') OR exit('No direct script access allowed');
  
class User_model extends MY_Model {
    
    function __construct()
    {
        parent::__construct();
        
        $this->_table      = 'users';
        $this->primary_key = 'user_id';
    }
    
    function get_group_list()
    {
        return $this->db->order_by('group_name ASC')->get('user_groups')->result_array();
    }
    
    function check_exist($username = '')
    {
        $u = $this->get_by('username', $username);
        
        if(isset($u->user_id) && $u->user_id > 0) return true;
        else return false;
    }
    
    function get_modules($group_id,$parent_id = 0){
    	$q	=	$this->db->select('m.name,m.title,m.id,m.parent_id,m.css_class,p.method')->from('modules as m')->join('permissions as p','m.id=p.module_id')->where('group_id',$group_id)->where('m.parent_id',$parent_id)->group_by('m.name,m.id')->order_by('m.order ASC')->get();
    	$rows	=	$q->result();
    	$q->free_result();
    	return $rows;
    }
    
    function check_module($group_id,$module_name = ''){
    	$q	=	$this->db->select('m.name')->from('modules as m')->join('permissions as p','m.id=p.module_id')->where('group_id',$group_id)->where('m.name',$module_name)->get();
    	$rows	=	$q->row();
    	if($rows)return TRUE;
    	return FALSE;
    }
    
    /**
     * @date 11.03.2011
     */
    function group_name($group_id = null)
    {
    	$r = $this->db->where('group_id', $group_id)->limit(1)->get('user_groups')->row();
    	return isset($r->group_name) ? $r->group_name : 'Not exist';
    }
}
